package com.wxzz.elearing.course.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.*;
import com.wxzz.elearing.course.entity.Exam;
import com.wxzz.elearing.course.entity.WxUserExam;
import com.wxzz.elearing.course.entity.WxUserExamHistory;
import com.wxzz.elearing.course.service.IExamServiceService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam")
@Api(tags = "在线考试(后台)")
public class ExamController {

    @Autowired
    private IExamServiceService examServiceService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "根据企业查询在线考试列表", notes = "根据企业查询在线考试列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listExamByEnterpriseId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Exam>>> listExamByEnterpriseId(@RequestHeader String token, @RequestBody ExamDto examDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return examServiceService.listExamByEnterpriseId(jsonObject.getLong("enterpriseId"), examDto);
    }

    @ApiOperation(value = "添加考试", notes = "添加考试", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveExam", method = RequestMethod.POST)
    public ReturnVO saveExam(@RequestHeader String token, @RequestBody Exam exam) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        exam.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return examServiceService.saveExam(exam);
    }

    @ApiOperation(value = "分配考试人", notes = "分配考试人", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveWxUserExam", method = RequestMethod.POST)
    public ReturnVO saveWxUserExam(@RequestBody WxUserExamInsDto wxUserExamInsDto) {
        return examServiceService.saveWxUserExam(wxUserExamInsDto);
    }

    @ApiOperation(value = "查询考试详情", notes = "查询考试详情", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectExamDetailById/{examId}", method = RequestMethod.POST)
    public ReturnVO<Exam> selectExamDetailById(@PathVariable Long examId) {
        return examServiceService.selectExamDetailById(examId);
    }

    @ApiOperation(value = "删除考试", notes = "删除考试", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteExam/{examId}", method = RequestMethod.POST)
    public ReturnVO<Exam> deleteExam(@PathVariable Long examId) {
        return examServiceService.deleteExam(examId);
    }

    @ApiOperation(value = "根据考试id查询考试人员", notes = "根据考试id查询考试人员", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectWxUserExamByExamId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserExam>>> selectWxUserExamByExamId(@RequestBody WxUserExamByExamIDto wxUserExamByExamIDto) {
        return examServiceService.selectWxUserExamByExamId(wxUserExamByExamIDto);
    }

    @ApiOperation(value = "查询该用户的在线考试", notes = "查询该用户的在线考试", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxListExamByEnterpriseId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Exam>>> wxListExamByEnterpriseId(@RequestBody ExamByWxUserIdDto examByWxUserIdDto) {
        return examServiceService.listExamByWxUserId(examByWxUserIdDto.getWxUserId(), examByWxUserIdDto);
    }

    @ApiOperation(value = "列出在线考试记录", notes = "列出在线考试记录", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listExamByExamHistory/{wxUserId}/{examId}", method = RequestMethod.POST)
    public ReturnVO<List<WxUserExamHistory>> listExamByExamHistory(@PathVariable Long wxUserId,@PathVariable Long examId) {
        return examServiceService.listExamByExamHistory(wxUserId, examId);
    }
}
